﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;



public partial class ViewStudentAssignmentByStudent : System.Web.UI.Page
{
    //public static int assId;
    //static object userId;
   public  string filePath;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //assId = Convert.ToInt32(Request.QueryString["AssignmentId"]);
            //userId = Session["UserId"];
            if (Request.QueryString["AssignmentId"] != null)
                setControlValues();
            Session["FilePath"] = "";
        }
        filePath = Session["FilePath"].ToString();
    }

    private void setControlValues()
    {
        DataSet dsAssignment = new DataSet();
        dsAssignment = AdminCreateTestDAL.GetAssignInfoByAssignId(Convert.ToInt32(Request.QueryString["AssignmentId"]));
        lablAssignmentName.Text = dsAssignment.Tables[0].Rows[0][1].ToString();
        lablSubject.Text = dsAssignment.Tables[0].Rows[0][2].ToString();
        lablTopic.Text = dsAssignment.Tables[0].Rows[0][3].ToString();
        lablStatus.Text=dsAssignment.Tables[1].Rows[0][3].ToString();

        DataTable dtHistor = new DataTable();
        dtHistor = AdminCreateTestDAL.GetAssignmentHistory(Convert.ToInt32(Request.QueryString["AssignmentId"]), Session["UserId"]);
        string text = "";
        for (int i = 0; i < dtHistor.Rows.Count; i++)
        {
            text+=dtHistor.Rows[i][0].ToString() + "<br/>";
            lablHistory.Text = text;
        }

    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }

    private void GetFile()
    {
        if (assignmentUploaded.HasFile)
        {
            string file = assignmentUploaded.PostedFile.FileName;
            string path = "~/Assignment/";
            assignmentUploaded.SaveAs(Server.MapPath(path) + file);
            Session["FilePath"] = "~/Assignment/" + file;
            filePath = Session["FilePath"].ToString();
        }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string remark="";
        //GetFile();
        DataTable dtUser=new DataTable();
        dtUser = AdminCreateTestDAL.GetUserName(Session["UserId"]);
        if(txtRemarks.Text!="")
         remark = dtUser.Rows[0][0].ToString() + ": " + txtRemarks.Text;
        AdminCreateTestDAL.SetAssignmentStatusStudent(Convert.ToInt32(Request.QueryString["AssignmentId"]), Session["UserId"],remark,filePath);

        DataTable dtEmail = new DataTable();
        dtEmail = AdminCreateTestDAL.GetAssignmentAttemptInfoForMail(Convert.ToInt32(Request.QueryString["AssignmentId"]));
        StringBuilder sbMail = new StringBuilder();
        sbMail.Append("<table width='70%'><tr><td>AssignmentId: " + dtEmail.Rows[0][0].ToString() + ".</td></tr>");
        sbMail.Append("<tr><td>Assignment Name: " + dtEmail.Rows[0][1].ToString() + ".</td></tr><tr><td></td></tr>");
        sbMail.Append("<tr><td>Subject Name: " + dtEmail.Rows[0][2].ToString() + ".</td></tr></table>");
        sbMail.Append("<tr><td>Topic Name:: " + dtEmail.Rows[0][3].ToString() + ".</td></tr></table>");
        sbMail.Append("<tr><td>Expiry Date: " + dtEmail.Rows[0][4].ToString() + ".</td></tr></table>");
        sbMail.Append("<tr><td>User Name: " + dtEmail.Rows[0][5].ToString() + ".</td></tr></table>");
        sbMail.Append("<tr><td><a href='http://www.skillify.net'>Uploded Assignment</a>: " + ".</td></tr></table>");



        ConfigurationManager.AppSettings["ContactEmailTo"] = "mannethegreat@gmail.com";
        if (SendEmail(ConfigurationManager.AppSettings["ContactEmailFrom"], ConfigurationManager.AppSettings["ContactEmailPassword"], ConfigurationManager.AppSettings["ContactEmailTo"], "Assignment Information", sbMail.ToString(), System.Web.Mail.MailFormat.Html, "") == true)
        {

        }
        clearAll();
    }

    public static bool SendEmail(string pGmailEmail, string pGmailPassword, string pTo, string pSubject, string pBody, System.Web.Mail.MailFormat pFormat, string pAttachmentPath)
    {
        try
        {
            System.Web.Mail.MailMessage myMail = new System.Web.Mail.MailMessage();
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver",
                              "smtp.gmail.com");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport",
                              "465");
            myMail.Fields.Add
                ("http://schemas.microsoft.com/cdo/configuration/sendusing",
                              "2");

            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //Use 0 for anonymous
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendusername",
                pGmailEmail);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/sendpassword",
                 pGmailPassword);
            myMail.Fields.Add
            ("http://schemas.microsoft.com/cdo/configuration/smtpusessl",
                 "true");
            myMail.From = pGmailEmail;
            myMail.To = pTo;
            myMail.Subject = pSubject;
            myMail.BodyFormat = pFormat;
            myMail.Body = pBody;


            System.Web.Mail.SmtpMail.SmtpServer = "smtp.gmail.com:465";
            System.Web.Mail.SmtpMail.Send(myMail);
            return true;
        }
        catch (Exception ex)
        {
            throw;
        }
    }


    private void clearAll()
    {
        txtRemarks.Text = "";
        assignmentUploaded.Controls.Clear();
        lablFile.Text = "";
        Session["FilePath"] = "";
        filePath = "";
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        try
        {
            GetFile();
            lablFile.Text = "File " + assignmentUploaded.PostedFile.FileName + " is uploaded successfully.";
        }
        catch
        {
            lablFile.Text = "Error while uploading file";
        }
    }
}